package com.baomidou.wechat.mp.apistore.actuator;

import org.apache.commons.lang.StringUtils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.baomidou.wechat.mp.apistore.vo.OilPriceVo;

/**
 * 今日油价 API 执行 Actuator
 */
public class OilPriceActuator extends ApiStoreActuator<OilPriceVo> {
	private static String API_OIL_PRICE = "showapi_open_bus/oil_price/find?prov=%s";
	private String prov;

	public static void main(String[] args) {
		ApiStoreActuator<OilPriceVo> actuator = new OilPriceActuator("北京");
		System.out.println(actuator.execute().toString());
	}

	protected OilPriceActuator() {
	}

	public OilPriceActuator(String prov) {
		this.prov = prov;
	}

	@Override
	public OilPriceVo execute() {
		String json = executeHttpGet(null, API_OIL_PRICE, getProv());
		if (StringUtils.isNotBlank(json)) {
			String body = JSON.parseObject(json).getString("showapi_res_body");
			if (StringUtils.isNotBlank(body)) {
				JSONArray list = JSON.parseObject(body).getJSONArray("list");
				if (list != null && list.size() >= 1) {
					return JSON.parseObject(list.get(0).toString(), OilPriceVo.class);
				}
			}
		}
		return null;
	}

	public String getProv() {
		return prov;
	}

	public void setProv(String prov) {
		this.prov = prov;
	}

}
